#region System
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CF.Framework.Engine.Drawing;
using CF.Framework.Engine.Effects;
using CF.Framework.Engine.Misc;
using CF.Framework.Engine.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GEffect = CF.Framework.Engine.Effects.Effect;
#endregion

namespace CF.Framework.Engine.Controls
{
	// Summary:
	//     Specifies the display state of an element.
	public enum Visibility
	{
		Visible = 0,
		Collapsed = 1,
	}

	public abstract class Control : GameObject
	{
		public virtual int Width { get; set; }
		public virtual int Height { get; set; }
		public virtual int Opacity { get; set; }
		public virtual Texture2D OpacityMask { get; set; }
		public virtual float Scale { get; set; }
		public virtual Vector2 Position { get; set; }
		public virtual Visibility Visibility { get; set; }
		public EffectCollection Effects { get; private set; }

		public Control()
		{
			this.Width = -1;
			this.Height = -1;
			this.Position = new Vector2(0, 0);
			this.Visibility = Visibility.Visible;
			this.Opacity = 255;
			this.Scale = 1;

			this.Effects = new EffectCollection(this);
		}

		protected Vector2 ScaledPosition
		{
			get
			{
				if (Scale == 1)
				{
					return Position;
				}

				int width = this.Width;
				int height = this.Height;
				if (width <= 0 || height <= 0)
				{
					return Position;
				}

				Vector2 newSize = new Vector2(width * Scale, height * Scale);
				return new Vector2(Position.X + (width - newSize.X) / 2, Position.Y + (height - newSize.Y) / 2);
			}
		}

		protected Rectangle? ScaledRectangle
		{
			get
			{
				if (Scale == 1)
				{
					return null;
				}

				int width = this.Width;
				int height = this.Height;
				if (width <= 0 || height <= 0)
				{
					return null;
				}

				Vector2 newSize = new Vector2(width * Scale, height * Scale);
				return new Rectangle((int)Position.X, (int)Position.Y, (int)newSize.X, (int)newSize.Y);
			}
		}

		internal override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
		{
			if (IsUnloading)
			{
				return;
			}

			base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

			foreach (var effect in this.Effects)
			{
				effect.Update(gameTime);
			}
		}

		internal override void Draw(GameTime gameTime)
		{
			if (IsUnloading
				|| this.Visibility == Controls.Visibility.Collapsed
				|| this.Opacity == 0
				|| this.Scale == 0)
			{
				return;
			}

			base.Draw(gameTime);
		}
	}
}
